//
//  VideoPlayViewController.m
//  iphoneLive
//
//  Created by 志刚杨 on 2017/10/18.
//  Copyright © 2017年 cat. All rights reserved.
//

#import "VideoPlayViewController.h"
#import <YYKit/YYKit.h>
//#import "sendButton.h"
//#import "CommitViewController.h"
#import "VideoDetailInfo.h"
//#import <YYKit/YYKit.h>
#import <QMUIKit/QMUIKit.h>
#import "XMNShareMenu.h"
#import "PlayVideoUserinfo.h"
//#import "VideoReportModel.h"
//#import "SDPriceStarView.h"
//#import "commentTableViewCell.h"
//#import "CommitViewControllerForAll.h"
#import "VideoTopHeader.h"
#import "VideoInfoModel.h"
#import "UserInfoViewController.h"
#import "ShortVideoLogic.h"
#import <UMSocialCore/UMSocialCore.h>
#import "ImageTitleButton.h"
#import "CallUserManger.h"
#import "GiftView.h"
#import "GiftLogic.h"
#import "VMsgMessage.h"
#import "LiveGiftShowCustom.h"
#define GIFTVIEW_TAG 10000
@interface VideoPlayViewController ()<YYTextKeyboardObserver,userinfodelegate,GiftViewDelegate,LiveGiftShowCustomDelegate,UIGestureRecognizerDelegate, UIScrollViewDelegate>
@property (nonatomic, assign) CGFloat tableViewOffsetYOnStartDrag;
@property(nonatomic, strong) UIButton *btnReturn;
@property(nonatomic, strong) UIButton *btnDislike;
@property(nonatomic, strong) UIButton *btnMore;
@property(nonatomic, strong) UIButton *btnSendMessage;
@property(nonatomic, strong) UIView *InputView;
@property(nonatomic, strong) UITextField *messageTextFiled;
@property(nonatomic, strong) UIView *toobarView;
@property(nonatomic, strong) QMUIButton *commit;//评论弹出列表
@property(nonatomic, strong) QMUIButton *sharebutton;//分享按钮
@property(nonatomic, strong) UIButton *maskView;
@property(nonatomic, strong) QMUIButton *videoFollow;//ic_short_video_follow icon_like_active
@property(nonatomic, strong) PlayVideoUserinfo *userinfo;
@property(nonatomic, assign) BOOL canTapHTTP;//点击是否请求666
@property(nonatomic, strong) ImageTitleButton *Playnumber;
@property(nonatomic, strong) NSMutableArray *ReplyArray;
@property(nonatomic, strong) NSString *touid;//回复uid
@property(nonatomic, strong) NSString *rid;
@property(nonatomic, strong) VideoTopHeader *topheader;
@property(nonatomic, strong) UILabel *video_title;
@property(nonatomic, strong) QMUIFillButton *Call;//与他视频聊聊
@property(nonatomic, strong) UIButton *headerImage;
@property(nonatomic, strong) UIButton *sendGift;

//礼物
@property(nonatomic, strong) GiftView *giftview;
@property(nonatomic, strong)  GiftLogic *giftLogic;
@property(nonatomic, strong) NSArray *giftList;
@property (nonatomic ,weak) LiveGiftShowCustom * customGiftShow;
//@property(nonatomic, assign) QMUIButton *
@end

@implementation VideoPlayViewController
{
    YYTextKeyboardManager *YYmanager;
    UITapGestureRecognizer * tapGesture;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setView];
    [self getInitData];
    self.canTapHTTP = NO;//先初始化一下避免
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    [IQKeyboardManager sharedManager].enable = NO;
    [UIApplication sharedApplication].idleTimerDisabled=YES;
}

-(void)initDataWithIndex:(NSInteger)index
{
    [super initDataWithIndex:index];
    self.model = self.videoList[index];
    [self getInitData];
    [self setView];
}

//设置键盘监听
-(void)setKeyBoard
{
    
}



//- (void)textFieldDidChange:(UITextField *)textField
//{
//    UITextRange * selectedRange = textField.markedTextRange;
//    if(selectedRange == nil || selectedRange.empty){
//        //这里取到textfielf.text最后的值 进行检索
//        NSString *text = textField.text;
//        if (text.length < 1) {
//            [self.sendButton setSelected:NO];
//
//        }
//        else {
//
//            [self.sendButton setSelected:YES];
//
//        }
//    }
//
//}

////键盘监听
//- (void)keyboardChangedWithTransition:(YYTextKeyboardTransition)transition {
//    CGRect toFrame =  [YYmanager convertRect:transition.toFrame toView:self.view];
//    BOOL toVisible = [YYmanager isKeyboardVisible];
//    NSLog(@"keuboadr status %d",toVisible);
//    if(toVisible == YES)
//    {
//        _toobarView.hidden = NO;
//        _toobarView.frame = CGRectMake(0, toFrame.origin.y - 50, SCREEN_WIDTH, 50);
//    }
//    else
//    {
//        if(_commitView.view.hidden == YES)
//        {
//            //隐藏toobar
//            [self setTooBarHidden];
//        }
//        else
//        {
//            [self setTooBarBottom];
//        }
//
//    }
//}

-(void)setTooBarHidden
{
    _toobarView.frame = CGRectMake(0, SCREEN_HEIGHT + 50, SCREEN_WIDTH, 50);
    _toobarView.hidden = YES;
}


-(void)setTooBarBottom
{
    _toobarView.frame = CGRectMake(0, SCREEN_HEIGHT - 50, SCREEN_WIDTH, 50);
    _toobarView.hidden = NO;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //    self.isHidenNaviBar = YES;
    self.navigationController.navigationBar.hidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
    [IQKeyboardManager sharedManager].enable = YES;
    self.navigationController.navigationBar.hidden = YES;
}


-(void)setView
{
    
    //先移除，再添加 避免ui重复
    [self.maskView removeFromSuperview];
    [self.btnReturn removeFromSuperview];
    [self.btnDislike removeFromSuperview];
    [self.btnMore removeFromSuperview];
    [self.btnSendMessage removeFromSuperview];
    [self.commit removeFromSuperview];
    [self.videoFollow removeFromSuperview];
    [self.Call removeFromSuperview];
    [self.sharebutton removeFromSuperview];
    [self.Playnumber removeFromSuperview];
    [self.userinfo removeFromSuperview];
    [self.video_title removeFromSuperview];
    [self.headerImage removeFromSuperview];
    [self.sendGift removeFromSuperview];
    
    self.view.userInteractionEnabled = YES;
    tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgClick)];
    tapGesture.delegate = self;
    [self.view addGestureRecognizer:tapGesture];
    
    self.maskView = [[UIButton alloc] init];
    self.maskView.frame = self.view.bounds;
    [self.maskView addTarget:self action:@selector(bgClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.maskView];
    self.maskView.hidden = YES;
    self.btnReturn = [[UIButton alloc] init];
    [self.btnReturn setImage:[UIImage imageNamed:@"icon_back_white"] forState:UIControlStateNormal];
    [self.btnReturn addTarget:self action:@selector(return) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btnReturn];
    
    self.btnDislike = [[UIButton alloc] init];
    self.btnDislike.hidden = NO;
    [self.btnDislike setImage:[UIImage imageNamed:@"sd_likeCount"] forState:UIControlStateNormal];
    [self.view addSubview:self.btnDislike];
    self.btnDislike.hidden = YES;
    
    self.btnMore = [[UIButton alloc] init];
    [self.btnMore setImage:[UIImage imageNamed:@"icon_more_regular"] forState:UIControlStateNormal];
    [self.view addSubview:self.btnMore];
    [self.btnMore addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
    self.btnMore.hidden = YES;
    self.btnSendMessage = [[UIButton alloc] init];
    
    self.btnSendMessage.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.btnSendMessage setImage:[UIImage imageNamed:@"fw_liveRoom_sendMsg"] forState:UIControlStateNormal];
    
    //    [self.btnSendMessage addTarget:self action:@selector(showCommit) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btnSendMessage];
    
    self.commit = [[QMUIButton alloc] init];
    [self.commit addTarget:self action:@selector(showCommit) forControlEvents:UIControlEventTouchUpInside];
    [self.commit setImage:[UIImage imageNamed:@"ic_short_video_chat"] forState:UIControlStateNormal];
    [self.commit setTitle:@"0" forState:UIControlStateNormal];
    [self.commit.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [self.commit setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.commit.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.commit setSpacingBetweenImageAndTitle:2];
    self.commit.hidden = YES;
    self.commit.backgroundColor = KRedColor;
    [self.view addSubview:self.commit];
    
    self.headerImage = [[UIButton alloc] init];
    ViewRadius(self.headerImage, 20);
    [self.headerImage addTarget:self action:@selector(pushUserInfoViewController) forControlEvents:UIControlEventTouchUpInside];
    self.headerImage.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:self.headerImage];
    
    self.sendGift = [[UIButton alloc] init];
    [self.sendGift setImage:IMAGE_NAMED(@"video_gift") forState:UIControlStateNormal];
    [self.sendGift addTarget:self action:@selector(showGiftView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.sendGift];
    
    [self.headerImage setImageWithURL:safeurl(self.model.avatar) forState:UIControlStateNormal placeholder:emptyimage];
    //    [self.headerImage setImage: forState:UIControlStateNormal];
    self.videoFollow = [[QMUIButton alloc] init];
    self.videoFollow.spacingBetweenImageAndTitle = 4;
    [self.videoFollow addTarget:self action:@selector(dofollow) forControlEvents:UIControlEventTouchUpInside];
    [self.videoFollow setImage:[UIImage imageNamed:@"video_like"] forState:UIControlStateNormal];
    [self.videoFollow setImage:[UIImage imageNamed:@"video_like_on"] forState:UIControlStateSelected];
    //    [self.videoFollow setTitle:@"0" forState:UIControlStateNormal];
    [self.videoFollow.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [self.videoFollow setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.videoFollow.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:self.videoFollow];
    
    self.Call = [[QMUIFillButton alloc] initWithFillColor:MainColor titleTextColor:KWhiteColor];
    [self.Call setTitle:ASLocalizedString(@"与TA视频聊聊")forState:UIControlStateNormal];
    self.Call.titleLabel.font = DEFAULT_FONT(13);
    kWeakSelf(self)
    [self.Call addTapBlock:^(UIButton *btn) {
        [weakself stopVideoPreview:YES];
        
        
        //
        [[CallUserManger sharedCallUserManger] CallUserV2WithUid:StringFromInt(weakself.model.uid) ViewControler:weakself];
//        UserInfoViewController *userVC = [UserInfoViewController new];
  //      userVC.uid = StringFromInt(weakself.model.uid);
    //    [weakself.navigationController pushViewController:userVC animated:YES];
    }];
    [self.view addSubview:self.Call];
    
    
    //    self.videoFollow.hidden = YES;
    
    
    //    __weak __typeof(self)weakSelf = self;
    //点击回复评论
    //
    //    self.commitView.didReplayClicked = ^(NSString *uid, NSString *username) {
    //        [weakSelf showBigReplyWithID:uid andRid:username];
    //    };
    //
    //
    //    self.commitView.view.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT/2);
    //    [self.view addSubview:self.commitView.view];
    
    //分享按钮
    
    self.sharebutton = [[QMUIButton alloc] init];
    [self.sharebutton setImage:[UIImage imageNamed:@"video_zhuanfa"] forState:UIControlStateNormal];
    [self.sharebutton.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [self.sharebutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.sharebutton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.sharebutton addTarget:self action:@selector(morebtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.sharebutton];
    
    //播放量
    self.Playnumber = [[ImageTitleButton alloc] initWithStyle:EImageTopTitleBottom maggin:UIEdgeInsetsMake(3, 3, 3, 3)];
    _Playnumber.imageSize = CGSizeMake(40, 40);
    _Playnumber.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    [self.Playnumber setImage:IMAGE_NAMED(@"video_liulan") forState:UIControlStateNormal];
    [self.sharebutton.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [self.sharebutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:self.Playnumber];
    [self.Playnumber.imageView setContentMode:UIViewContentModeScaleAspectFit];
    [self setKeyBoard];
    
    self.userinfo = [[PlayVideoUserinfo alloc] init];
    [self.userinfo setModel:self.model];
    self.userinfo.delegate = self;
    [self.view addSubview:self.userinfo];
    self.userinfo.hidden = YES;
    
    self.video_title = [[UILabel alloc] init];
    self.video_title.font = DEFAULT_FONT(16);
    self.video_title.textColor = [UIColor whiteColor];
    self.video_title.text = self.model.title;
    [self.view addSubview:self.video_title];
    //设置约束
    [self setLayout];
}

//跳转到个人主页
- (void)pushUserInfoViewController {
    [self stopVideoPreview:YES];
    UserInfoViewController *userVC =
    [UserInfoViewController new];
    userVC.uid = StringFromInt(self.model.uid);
    [self.navigationController pushViewController:userVC animated:YES];
}

-(void)handleSingleTap:(UITapGestureRecognizer *)sender{
    CGPoint point = [sender locationInView:self.view];
    NSLog(@"handleSingleTap!pointx:%f,y:%f",point.x,point.y);
}



- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    CGPoint currentPoint = [gestureRecognizer locationInView:nil];
    //    if (CGRectContainsPoint(self.giftview.bounds, currentPoint)) {
    //        return NO;
    //    }
    
    return YES;
    
    
    
    
    //
    //    if (touch.view.tag == GIFTVIEW_TAG) {
    //        return NO;
    //    }
    //
    //    return YES; // handle the touch
    
}

//心动
-(void)dofollow
{
    ShortVideoLogic *videoLogic = [[ShortVideoLogic alloc] init];
    [videoLogic followVideo:self.model :^{
        
        if(self.videoFollow.selected)
        {
            self.videoFollow.selected = NO;
            self.canTapHTTP = NO;
        }
        else
        {
            self.videoFollow.selected = YES;
            self.canTapHTTP = YES;
        }
    }];
}

-(void)bgClick
{
    //    if(_giftview.hidden == NO)
    //    {
    //        tapGesture.enabled = NO;
    //    }
    //    else
    //    {
    //        tapGesture.enabled = YES;
    //    }
    
    self.messageTextFiled.placeholder = ASLocalizedString(@"说点什么...");
    //    self.userinfo.hidden = NO;
    [self.messageTextFiled resignFirstResponder];
    [self setTooBarHidden];
    _giftview.hidden = YES;
    self.maskView.hidden = YES;
}

//return
-(void)return
{
    [self stopVideoPreview:YES];
    //    [self.livePlayer resume];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)showInputView
{
    [self.messageTextFiled becomeFirstResponder];
}

-(void)setLayout
{
    [self.btnReturn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(30);
        make.left.equalTo(self.view).offset(10);
        make.width.height.equalTo(@60);
        make.height.equalTo(@60);
    }];
    
    [self.Call mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-10);
        make.right.equalTo(self.view).offset(-10);
        make.width.equalTo(@100);
        make.height.equalTo(@44);
    }];
    
    self.btnDislike.hidden = YES;
    [self.btnDislike mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.and.height.equalTo(@50);
        //        make.top.equalTo(self.btnReturn);
        make.centerY.equalTo(self.btnReturn);
        make.left.equalTo(self.btnReturn.mas_right).offset(5);
    }];
    
    [self.btnMore mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-20);
        make.centerY.equalTo(self.btnReturn);
        make.width.and.height.equalTo(@50);
    }];
    
    
    self.btnMore.hidden = YES;
    //底部  说点什么 发言按钮
    [self.btnSendMessage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-15);
        make.left.equalTo(self.view).offset(10);
        make.height.equalTo(@60);
        make.width.equalTo(@60);
    }];
    
#pragma mark - toobar
    //视频喜欢
    [self.videoFollow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_sharebutton);
        make.height.equalTo(_sharebutton);
        make.width.equalTo(_sharebutton);
        
        make.bottom.equalTo(self.Playnumber.mas_top).offset(-15);
    }];
    
    [self.sharebutton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.Call.mas_top).offset(-20);
        make.right.equalTo(self.view).offset(-15);
        make.width.equalTo(@40);
        make.height.equalTo(@60);
    }];
    //
    [self.video_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(10);
        make.bottom.equalTo(self.view).offset(-40);
    }];
    
    //底部用户信息
    [self.userinfo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(self.view).offset(30);
        make.width.equalTo(@140);
        make.height.equalTo(@120);
    }];
    
    //播放量
    [self.Playnumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.sharebutton);
        make.width.and.height.equalTo(self.sharebutton);
        
        make.bottom.equalTo(self.sharebutton.mas_top).offset(-15);
    }];
    
    [self.commit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom);
        make.width.and.height.equalTo(@100);
    }];
    
    [_headerImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_sharebutton);
        make.bottom.equalTo(_videoFollow.mas_top).offset(-25);
        make.width.equalTo(@40);
        make.height.equalTo(@40);
    }];
    
    [self.sendGift mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_sharebutton);
        make.bottom.equalTo(_headerImage.mas_top).offset(-25);
        make.width.equalTo(@40);
        make.height.equalTo(@40);
    }];
    
}

-(void)getInitData
{
    //    self.touid = self.model.user_id;
    
    
    _giftLogic = [GiftLogic new];
    [_giftLogic getGiftListHookData:^(NSArray *giftList) {
        _giftList = giftList;
        _giftview = [[GiftView alloc]initWithShareViewToView:self.view AndGift:_giftList];
        _giftview.tag = GIFTVIEW_TAG;
        _giftview.delegate = self;
        _giftview.hidden = YES;
        [self.view addSubview:_giftview];
        //        _giftview.hidden = YES;
    }];
    
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"short_video_api" A:@"get_video"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:@(self.model.id) forKey:@"video_id"];
    
    
    [CYNET POSTv3:url parameters:param responseCache:^(id responseObject) {
        
    } success:^(id responseObject) {
        self.videoinfo = [VideoInfoModel modelWithDictionary:responseObject];
        if(self.videoinfo.is_follow == 0)
        {
            self.videoFollow.selected = NO;
        }
        else
        {
            self.videoFollow.selected = YES;
        }
        [self.Playnumber setTitle:self.videoinfo.host_count forState:UIControlStateNormal];
        [self startVideoPreview:YES];
        self.giftLogic.touid = StringFromInt(self.model.id);
        
    } failure:^(NSString *error, NSInteger code) {
        if(code == 10020)
        {
            NSString *buyTip = [NSString stringWithFormat:ASLocalizedString(@"此视频为付费视频，需要消耗%@%@，是否购买"),self.model.coin,KCOIN_NAME];
            QMUIAlertController *alert = [[QMUIAlertController alloc] initWithTitle:ASLocalizedString(@"提示")message:buyTip preferredStyle:QMUIAlertControllerStyleAlert];
            [alert addAction:[QMUIAlertAction actionWithTitle:ASLocalizedString(@"取消")style:QMUIAlertActionStyleCancel handler:^(QMUIAlertAction *action) {
                
            }]];
            
            [alert addAction:[QMUIAlertAction actionWithTitle:ASLocalizedString(@"购买")style:QMUIAlertActionStyleDefault handler:^(QMUIAlertAction *action) {
                [self buyVideoViewWithModel:self.model];
            }]];
            [alert showWithAnimated:YES];
            
        }
    } hasCache:NO];
}

-(void)buyVideoViewWithModel:(videoListModel *)model
{
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"short_video_api" A:@"buy_video"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [[HUDHelper sharedInstance] syncLoading];
    [param setObject:StringFromInt(model.id) forKey:@"video_id"];
    
    [CYNET POSTv2:url parameters:param responseCache:^(id responseObject) {
        
    } success:^(id responseObject) {
        [[HUDHelper sharedInstance] syncStopLoading];
        [self getInitData];
    } failure:^(NSString *error) {
        [[HUDHelper sharedInstance] syncStopLoading];
        [MBProgressHUD showTopTipMessage:error];
    } hasCache:NO];
    
}


-(void)initViewData
{
    if([self.model.like isEqualToString:@"1"])
    {
        self.videoFollow.selected = YES;
    }
    else
    {
        self.videoFollow.selected = NO;
        self.canTapHTTP = YES;
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

-(void)morebtnClick
{
    QMUIAlertController *alert = [QMUIAlertController alertControllerWithTitle:ASLocalizedString(@"更多操作")message:@""  preferredStyle:QMUIAlertControllerStyleActionSheet];
    
    [alert addAction:[QMUIAlertAction actionWithTitle:ASLocalizedString(@"分享")style:QMUIAlertActionStyleDefault handler:^(QMUIAlertAction *action) {
        [self shareAction];
    }]];
    //判断如果是自己的视频添加一下删除按钮
    //    if([self.model.user_id isEqualToString:[IMAPlatform sharedInstance].host.imUserId])
    //    {
    //        [alert addAction:[QMUIAlertAction actionWithTitle:ASLocalizedString(@"删除")style:QMUIAlertActionStyleDefault handler:^(QMUIAlertAction *action) {
    //            [self doDelVideo];
    //        }]];
    //    }
    
    [alert showWithAnimated:YES];
}

- (void)doDelVideo {
    QMUIAlertController *alert = [[QMUIAlertController alloc] initWithTitle:ASLocalizedString(@"温馨提示")message:ASLocalizedString(@"真的要删除此视频吗")preferredStyle:QMUIAlertControllerStyleAlert];
    
    [alert addAction:[QMUIAlertAction actionWithTitle:ASLocalizedString(@"取消")style:QMUIAlertActionStyleCancel handler:^(QMUIAlertAction *action) {
        
    }]];
    
    //    [alert addAction:[QMUIAlertAction actionWithTitle:ASLocalizedString(@"删除")style:QMUIAlertActionStyleDestructive handler:^(QMUIAlertAction *action) {
    //        [[FWHUDHelper sharedInstance] syncLoading];
    //        NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
    //        [parmDict setObject:@"short_video" forKey:@"ctl"];
    //        [parmDict setObject:@"del_short_video" forKey:@"act"];
    //        [parmDict setObject:self.model.id forKey:@"video_id"];
    //        [[NetHttpsManager manager] POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
    //         {
    //             [[FWHUDHelper sharedInstance] syncStopLoading];
    //             if ([responseJson toInt:@"status"] == 1)
    //             {
    //                 [APP_DELEGATE popViewController];
    //             }
    //             else
    //             {
    //                 [[FWHUDHelper sharedInstance]tipMessage:[responseJson toString:@"error"]];
    //             }
    //         } FailureBlock:^(NSError *error)
    //         {
    //             [[FWHUDHelper sharedInstance]tipMessage:[NSString stringWithFormat:@"%@",error]];
    //         }];
    //    }]];
    
    
    
    [alert showWithAnimated:YES];
    
}

- (void)shareAction {
    //分享媒介数据源
    //    NSMutableArray *shareAry = @[@{kXMNShareImage:@"more_chat",
    //                            kXMNShareHighlightImage:@"more_chat_highlighted",
    //                            kXMNShareTitle:ASLocalizedString(@"私信和群")},
    //                          @{kXMNShareImage:@"more_weixin",
    //                            kXMNShareHighlightImage:@"more_weixin_highlighted",
    //                            kXMNShareTitle:ASLocalizedString(@"微信好友")},
    //                          @{kXMNShareImage:@"more_circlefriends",
    //                            kXMNShareHighlightImage:@"more_circlefriends_highlighted",
    //                            kXMNShareTitle:ASLocalizedString(@"朋友圈")},
    //                          @{kXMNShareImage:@"more_icon_zhifubao",
    //                            kXMNShareHighlightImage:@"more_icon_zhifubao_highlighted",
    //                            kXMNShareTitle:ASLocalizedString(@"支付宝好友")},
    //                          @{kXMNShareImage:@"more_icon_zhifubao_friend",
    //                            kXMNShareHighlightImage:@"more_icon_zhifubao_friend_highlighted",
    //                            kXMNShareTitle:ASLocalizedString(@"生活圈")},
    //                          @{kXMNShareImage:@"more_icon_qq",
    //                            kXMNShareHighlightImage:@"more_icon_qq_highlighted",
    //                            kXMNShareTitle:@"QQ"},
    //                          @{kXMNShareImage:@"more_icon_qzone",
    //                            kXMNShareHighlightImage:@"more_icon_qzone_highlighted",
    //                            kXMNShareTitle:ASLocalizedString(@"QQ空间")},
    //                          @{kXMNShareImage:@"more_mms",
    //                            kXMNShareHighlightImage:@"more_mms_highlighted",
    //                            kXMNShareTitle:ASLocalizedString(@"短信")},
    //                          @{kXMNShareImage:@"more_email",
    //                            kXMNShareHighlightImage:@"more_email_highlighted",
    //                            kXMNShareTitle:ASLocalizedString(@"邮件分享")},
    //                          @{kXMNShareImage:@"more_icon_cardbackground",
    //                            kXMNShareHighlightImage:@"more_icon_cardbackground_highlighted",
    //                            kXMNShareTitle:ASLocalizedString(@"设卡片背景")},
    //                          @{kXMNShareImage:@"more_icon_collection",
    //                            kXMNShareTitle:ASLocalizedString(@"收藏")},
    //                          @{kXMNShareImage:@"more_icon_topline",
    //                            kXMNShareTitle:ASLocalizedString(@"帮上头条")},
    //                          @{kXMNShareImage:@"more_icon_link",
    //                            kXMNShareTitle:ASLocalizedString(@"复制链接")},
    //                          @{kXMNShareImage:@"more_icon_report",
    //                            kXMNShareTitle:ASLocalizedString(@"举报")},
    //                          @{kXMNShareImage:@"more_icon_back",
    //                            kXMNShareTitle:ASLocalizedString(@"返回首页")}];
    
    
    //    //分享媒介数据源
    //    NSArray* shareAry = @[@{kXMNShareImage:@"more_weixin",
    //                   kXMNShareHighlightImage:@"more_weixin_highlighted",
    //                   kXMNShareTitle:ASLocalizedString(@"微信好友")},
    //                 @{kXMNShareImage:@"more_circlefriends",
    //                   kXMNShareHighlightImage:@"more_circlefriends_highlighted",
    //                   kXMNShareTitle:ASLocalizedString(@"朋友圈")},
    //                @{kXMNShareImage:@"more_icon_qq",
    //                  kXMNShareHighlightImage:@"more_icon_qq_highlighted",
    //                  kXMNShareTitle:@"QQ"},
    //                @{kXMNShareImage:@"more_icon_qzone",
    //                  kXMNShareHighlightImage:@"more_icon_qzone_highlighted",
    //                  kXMNShareTitle:ASLocalizedString(@"QQ空间")},
    //                 @{kXMNShareImage:@"more_icon_link",
    //                   kXMNShareTitle:ASLocalizedString(@"复制链接")},
    //                @{kXMNShareImage:@"more_icon_report",
    //                  kXMNShareTitle:ASLocalizedString(@"举报")},
    //                 ];
    
    NSArray* shareAry = @[@{kXMNShareImage:@"more_weixin",
                            kXMNShareHighlightImage:@"more_weixin_highlighted",
                            kXMNShareTitle:ASLocalizedString(@"微信好友")}
                          ];
    
    
    NSMutableArray *fullarr = [NSMutableArray arrayWithArray:shareAry];
    
    //如果是自己显示删除按钮
    //    if([self.model.uid isEqualToString:[Config getOwnID]])
    //    {
    //        [fullarr addObject:@{kXMNShareImage:@"ic_short_video_del",
    //                             kXMNShareHighlightImage:@"ic_short_video_del",
    //                             kXMNShareTitle:ASLocalizedString(@"删除")}];
    //    }
    
    //自定义头部
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 36)];
    headerView.backgroundColor = [UIColor clearColor];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(16, 21, headerView.frame.size.width-32, 15)];
    label.textColor = [UIColor colorWithRed:94/255.0 green:94/255.0 blue:94/255.0 alpha:1.0];;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:15];
    label.text = ASLocalizedString(@"操作");
    [headerView addSubview:label];
    
    XMNShareView *shareView = [[XMNShareView alloc] init];
    //设置头部View 如果不设置则不显示头部
    shareView.headerView = headerView;
    
    __weak __typeof(shareView)weakshareView = shareView;
    
    [shareView setSelectedBlock:^(NSUInteger tag, NSString *title) {
        if([title isEqualToString:ASLocalizedString(@"复制链接")])
        {
            [self copyUrl];
        }
        else if([title isEqualToString:ASLocalizedString(@"删除")])
        {
            [self stopVideoPreview:YES];
            [self delVideo];
        }
        else if([title isEqualToString:@"QQ"])
        {
            //            [self simplyShare:SSDKPlatformSubTypeQQFriend];
        }
        else if([title isEqualToString:ASLocalizedString(@"QQ空间")])
        {
            //            [self simplyShare:SSDKPlatformSubTypeQZone];
            
            
        }
        else if([title isEqualToString:ASLocalizedString(@"微信好友")])
        {
            //            [[ShareManager sharedShareManager] shareImageToPlatformType:UMSocialPlatformType_WechatTimeLine WidthImage:[UIImage imageNamed:ASLocalizedString(@"默认头像")]];
            [[ShareManager sharedShareManager] shareImageToPlatformType:UMSocialPlatformType_WechatSession WidthImage:[UIImage imageNamed:ASLocalizedString(@"默认头像")] withVC:self];
            //            [self simplyShare:SSDKPlatformSubTypeWechatSession];
            
        }
        else if([title isEqualToString:ASLocalizedString(@"朋友圈")])
        {
            //            [self simplyShare:SSDKPlatformSubTypeWechatTimeline];
        }
        else if([title isEqualToString:ASLocalizedString(@"举报")])
        {
            //            [self ReportView];
            
        }
        NSLog(@"\ntag :%lu  \ntitle :%@",(unsigned long)tag,title);
        [weakshareView dismissUseAnimated:NO];
    }];
    
    //计算高度 根据第一行显示的数量和总数,可以确定显示一行还是两行,最多显示2行
    [shareView setupShareViewWithItems:fullarr];
    
    [shareView showUseAnimated:YES];
}

//-(void)ReportView
//{
//    NSMutableArray *tmparr = [NSMutableArray array];
//    for (int i = 0 ;i<_ReplyArray.count;i++) {
//        VideoReportModel *model = _ReplyArray[i];
//        NSDictionary *tmpdic = @{@"title":model.name,@"value":model.id};
//        [tmparr addObject:tmpdic];
//    }
//    SDPriceStarView *sdpv = [SDPriceStarView sdPriceStarViewWith:tmparr];
//
//    sdpv.sdPriceBlock = ^(NSString *priceID) {
//
//    };
//
//    sdpv.sdSureBlock = ^(NSString *pid) {
////        if(pid.length >0)
////        {
////            NSString *url = [Utils makeUrlWithM:@"ShortVideo" andA:@"requestShortVideoReport"];
////            NSDictionary *params = @{
////                                     @"uid":[Config getOwnID],
////                                     @"token":[Config getOwnToken],
////                                     @"vid":self.model.id,
////                                     @"rid":pid
////                                     };
////            [WZXNetworking getDicWithUrl:url params:params success:^(NSDictionary *dic) {
////                [MBProgressHUD showSuccess:ASLocalizedString(@"举报成功")];
////
////            } fail:^(NSString *error) {
////                [MBProgressHUD showError:error];
////
////            }];
////        }
////        else
////        {
////            return;
////        }
//
//    };
//
//    [self.view addSubview:sdpv];
//}

-(void)copyUrl
{
    //    NSString *url = [NSString stringWithFormat:@"%@/index.php?g=Live&m=Stream&a=index&id=%@",h5url,self.model.id];
    //    [[UIPasteboard generalPasteboard] setString:url];
    //    [SVProgressHUD showSuccessWithStatus:ASLocalizedString(@"已复制到粘贴板")];
}

-(void)delVideo
{
    //    NSString *url = [Utils makeUrlWithM:@"ShortVideo" andA:@"requestShortVideoDel"];
    //    NSDictionary *params = @{
    //                             @"uid":[Config getOwnID],
    //                             @"token":[Config getOwnToken],
    //                             @"vid":self.model.id
    //                             };
    //    [WZXNetworking getDicWithUrl:url params:params success:^(NSDictionary *dic) {
    //        [SVProgressHUD showSuccessWithStatus:ASLocalizedString(@"删除成功")];
    //        [self.navigationController popViewControllerAnimated:YES];
    //    } fail:^(NSString *error) {
    //        [SVProgressHUD showErrorWithStatus:error];
    //    }];
    
}

/**
 *  简单分享
 */
- (void)simplyShare:(int)SSDKPlatformType
{
    
    /**
     * 在简单分享中，只要设置共有分享参数即可分享到任意的社交平台
     **/
    
    //
    //    NSString *url = [NSString stringWithFormat:@"%@/index.php?g=Live&m=Stream&a=index&id=%@",h5url,self.model.id];
    //
    //
    //    int SSDKContentType = SSDKContentTypeAuto;
    //
    //    NSURL *ParamsURL;
    //
    //    if(SSDKPlatformType == SSDKPlatformTypeSinaWeibo)
    //    {
    //        SSDKContentType = SSDKContentTypeImage;
    //    }
    //    else if((SSDKPlatformType == SSDKPlatformSubTypeWechatSession || SSDKPlatformType == SSDKPlatformSubTypeWechatTimeline))
    //    {
    //拼装分享地址
    //        NSString *strFullUrl = url;
    //        ParamsURL = [NSURL URLWithString:strFullUrl];
    //    }
    //    else{//app_ios
    //        ParamsURL = [NSURL URLWithString:[common app_ios]];
    //    }
    //判断是不是微信分享
    //创建分享参数
    //    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    //
    //    [shareParams SSDKSetupShareParamsByText:ASLocalizedString(@"分享")//                                     images:self.model.cover_url
    //                                        url:ParamsURL
    //                                      title:[common share_title]
    //                                       type:SSDKContentType];
    //
    //    //进行分享
    //    [ShareSDK share:SSDKPlatformType
    //         parameters:shareParams
    //     onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
    //         switch (state) {
    //             case SSDKResponseStateSuccess:
    //             {
    //
    //                 NSString *url = [Utils makeUrlWithM:@"ShortVideo" andA:@"requestShareNumAdd"];
    //                 NSDictionary *params =
    //                 @{
    //                   @"uid":[Config getOwnID],
    //                   @"token":[Config getOwnToken],
    //                   @"vid":self.model.id
    //                   };
    //                 [WZXNetworking getDicWithUrl:url params:params success:^(id dic) {
    //                     [self getInitData];
    //                 } fail:^(NSString *error) {
    //
    //                 }];
    //
    //                 [MBProgressHUD showSuccess:ASLocalizedString(@"分享成功")];
    //                 break;
    //             }
    //             case SSDKResponseStateFail:
    //             {
    //                 [MBProgressHUD showError:ASLocalizedString(@"分享失败")];
    //
    //
    //
    //                 break;
    //
    //             }
    //             case SSDKResponseStateCancel:
    //             {
    //                 break;
    //             }
    //             default:
    //                 break;
    //         }
    //     }];
    
}
- (void)showCommit {
    [self showBigReplyWithID:@"0" andRid:StringFromInt(self.model.id)];
}

//
//展示大的评论弹窗
-(void)showBigReplyWithID:(NSString *)uid andRid:(NSString *)rid
{
    //    CommitViewControllerForAll* bigcommit = [[CommitViewControllerForAll alloc] init];
    //    bigcommit.model = self.model;
    //    bigcommit.rid = rid;
    //    [APP_DELEGATE pushViewController:bigcommit];
    //   [[TCBaseAppDelegate sharedAppDelegate] pushViewController:bigcommit animated:YES];
    //    bigcommit.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 100);
    //    [self.view addSubview:bigcommit.view];
}


//用户信息点击代理
-(void)clickHeader
{
    [self stopVideoPreview:YES];
    //    personMessage *vc = [[personMessage alloc] init];
    //    vc.userID = self.model.uid;
    //    [[TCBaseAppDelegate sharedAppDelegate] pushViewController:vc animated:NO];
    
}

- (BOOL)prefersStatusBarHidden{
    return YES;
}

//礼物

- (void)showGiftView {
    //显示gift
    _maskView.hidden = NO;
    _giftview.hidden = NO;
    tapGesture.enabled = NO;
}



#pragma mark - 礼物选择发送代理
-(void)giftSendClickWithInfo:(GiftModel *)model AndNumber:(NSInteger)number
{
    if(number == 0)
    {
        [[HUDHelper sharedInstance] tipMessage:ASLocalizedString(@"需要选择发送礼物数量")];
        return;
    }
    self.giftLogic.touid = StringFromInt(self.model.uid);
    [self.giftLogic sendGiftWith:model AndNumber:number Hook:^(BOOL sendSuccess, NSString *error, TIMMessage *message) {
        
        if(sendSuccess)
        {
            
        }
        else
        {
            NSLog(ASLocalizedString(@"发送失败，原因%@"),error);
        }
    }];
    
    NSLog(ASLocalizedString(@"选择发送的礼物是%@"),model.name);
}


/*
 礼物视图支持很多配置属性，开发者按需选择。
 */
- (LiveGiftShowCustom *)customGiftShow{
    if (!_customGiftShow) {
        _customGiftShow = [LiveGiftShowCustom addToView:self.view];
        _customGiftShow.addMode = LiveGiftAddModeAdd;
        [_customGiftShow setMaxGiftCount:3];
        [_customGiftShow setShowMode:LiveGiftShowModeFromTopToBottom];
        [_customGiftShow setAppearModel:LiveGiftAppearModeLeft];
        [_customGiftShow setHiddenModel:LiveGiftHiddenModeNone];
        [_customGiftShow enableInterfaceDebug:YES];
        _customGiftShow.delegate = self;
        _customGiftShow.backgroundColor = RGB(33, 33, 33);
    }
    return _customGiftShow;
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    self.tableViewOffsetYOnStartDrag = scrollView.contentOffset.y;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (decelerate == NO) {
        [self handlePlayWhenEndDrag];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self handlePlayWhenEndDrag];
}

- (void)handlePlayWhenEndDrag {
    CGFloat distence = self.tableView.contentOffset.y - self.tableViewOffsetYOnStartDrag;
    if (distence <= 44 && distence >= -54) return;
    self.model = self.videoList[self.currentIndex];
    [self setView];
    [self getInitData];
}

@end

